package cn.hfjava.kz.service.impl;

import cn.hfjava.kz.entity.dto.UserQueryDTO;
import cn.hfjava.kz.entity.vo.PageVO;
import cn.hfjava.kz.entity.vo.UserVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hfjava.kz.entity.po.User;
import cn.hfjava.kz.service.UserService;
import cn.hfjava.kz.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lds
 * @description 针对表【users(用户表)】的数据库操作Service实现
 * @createDate 2024-07-09 11:22:19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    private final ModelMapper modelMapper;

    public UserServiceImpl(ModelMapper modelMapper) {
        this.modelMapper = modelMapper;
    }

    @Override
    public PageVO<UserVO> findByPage(UserQueryDTO userQueryDto) {
        PageHelper.startPage(userQueryDto.getCurPage(), userQueryDto.getPageSize());
        List<UserVO> userVoList = userMapper.getUserVoList(userQueryDto);
        long total = userMapper.getUserVoCount(userQueryDto);
        // 这里明确指定泛型类型参数为 UserVO
        PageVO pageVO = new PageVO<>(userQueryDto.getCurPage(), userQueryDto.getPageSize(), total, userVoList);
        return pageVO;
    }

    @Override
    public List<User> findUsersByRoleId(Integer roleId) {
        return userMapper.findUsersByRoleId(roleId);
    }

    @Override
    public List<UserVO> selectList() {
        List<User> userList = userMapper.selectUser();
        return userList.stream()
                .map(user -> modelMapper.map(user, UserVO.class))
                .collect(Collectors.toList());
    }

}
